/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cxa.cableModemMonitor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Christopher Armenio
 *
 */
public abstract class CableModem
{
	private static final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy HH:mm");
	
	protected List<UpstreamChannelReading> chanReadings_upstream = null;
	protected List<DownstreamChannelReading> chanReadings_downstream = null;
	protected Date dateOfLastReading = null;
	
	
	public List<UpstreamChannelReading> getUpstreamChannelReadings()
	{
		return this.chanReadings_upstream;
	}
	
	
	public List<DownstreamChannelReading> getDownstreamChannelReadings()
	{
		return this.chanReadings_downstream;
	}
	
	
	public Date getDateOfLastReading()
	{
		return this.dateOfLastReading;
	}
	
	
	public String getCsvString_header()
	{
		StringBuilder sb = new StringBuilder();
		if( (this.chanReadings_downstream == null) || (this.chanReadings_upstream == null) || (this.dateOfLastReading == null) ) this.updateChannelReadings();
		
		sb.append("Date/Time, ");
		
		for( int i = 0; i < this.chanReadings_upstream.size(); i++ )
		{
			sb.append(UpstreamChannelReading.getCsvHeaderString());
			sb.append(", ");
		}
		
		for( int i = 0; i < this.chanReadings_downstream.size(); i++ )
		{
			sb.append(DownstreamChannelReading.getCsvHeaderString());
			if( i != (this.chanReadings_downstream.size()-1) ) sb.append(", ");
		}
		
		return sb.toString();
	}
	
	
	public String getCsvString_reading()
	{
		StringBuilder sb = new StringBuilder();
		
		sb.append(SDF.format(this.dateOfLastReading) + ", ");
		
		for( int i = 0; i < this.chanReadings_upstream.size(); i++ )
		{
			ChannelReading currReading = this.chanReadings_upstream.get(i);
			sb.append(currReading.getCsvString());
			sb.append(", ");
		}
		
		for( int i = 0; i < this.chanReadings_downstream.size(); i++ )
		{
			ChannelReading currReading = this.chanReadings_downstream.get(i);
			sb.append(currReading.getCsvString());
			if( i != (this.chanReadings_downstream.size()-1) ) sb.append(", ");
		}
		
		return sb.toString();
	}
	
	
	public abstract void updateChannelReadings();
}